﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;


namespace Web.Filter
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
    public class UserPermissionsFilter : BaseFilter, IActionFilter
    {
        private const string LoginUrl = "~/Home/Login";

        

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
        }

        /// <summary>
        ///     Action访问前调用，检查Userid是否为空，为空未登录
        /// </summary>
        /// <param name="filterContext"></param>
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {

            
            Init(filterContext);
            LoggingVist();
            if (IsVailPath())
                return;
            /*
            if (filterContext.ActionParameters.Count == 0)//传递参数为0，非操作Action
                return;
                
            // string actionName = filterContext.ActionDescriptor.ActionName;
            // string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;


            if (HttpContext.Current.Session[Base.UserKey] == null)
            {
                //string ReferrerUrl = filterContext.RequestContext.HttpContext.Request.UrlReferrer.AbsoluteUri;
                filterContext.Result = new RedirectResult(LoginUrl);
                //filterContext.Controller.TempData[Base.ErrorInfo] = MessageResources.AccountSessionIsLost;
            }
            else
            {
                // HttpContext.Current.Session[Base.UserInfo] =
                //   MemService.KeepSessionInfo((Guid)HttpContext.Current.Session[Base.UserKey]).ReData;
            }
             * 
            /*
            Results result = service.oerationAuthorityVerification(userid, controllerName, actionName);
             */

        }

        /// <summary>
        ///     是否为验证路径
        /// </summary>
        /// <returns></returns>
        private bool IsVailPath()
        {
            return ControllerDescriptor.ControllerName == "Home" ||
                   ControllerDescriptor.ControllerName == "Admin";
        }

        
    }



}